Ext.namespace("TR.admin.trManage");

TR.admin.trManage.item = function(panelId, title, iconClz){
	var itemStore = new Ext.data.JsonStore({
		url : "admin/item/listByTest",
		fields : ["id", "testName", "innerNumber", "innerType", "question",
			"choiceA", "choiceB", "choiceC", "choiceD", "choiceE", "choiceF"
		],
		totalProperty : "total",
		root : "rows",
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					testId : testCombo.getValue()
				});
			}
		}
	});
	var itemSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var itemCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "测类类型",
		dataIndex : "testName"
	}, {
		header : "内部序号",
		dataIndex : "innerNumber",
		width : 60
	}, {
		header : "内部类型",
		dataIndex : "innerType"
	}, {
		header : "题目",
		dataIndex : "question",
		width : 280
	}, {
		header : "选项A",
		dataIndex : "choiceA"
	}, {
		header : "选项B",
		dataIndex : "choiceB"
	}, {
		header : "选项C",
		dataIndex : "choiceC"
	}, {
		header : "选项D",
		dataIndex : "choiceD"
	}]);

	var itemGrid = new Ext.grid.GridPanel({
		store : itemStore,
		cm : itemCm,
		sm : itemSm,
		frame : true
	});

	var testCombo = genTestCombo();

	var queryBar = ["测类类型:", testCombo, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			itemStore.reload();
		}
	}];

	var optBar = [{
		text : "导入",
		iconCls : "import",
		handler : importIn
	}];

	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "hbox",
		layoutConfig : {
			align : "stretch"
		},
		items : [itemGrid],
		tbar : genToolBar(optBar, queryBar, panelId)
	});
	function genAddorUpdateWin(url){
		return new Ext.Window({
			title : "新增或修改试题",
			layout : "fit",
			modal : true,
			iconCls : "save",
			height : 535,
			width : 770,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
	}

	function del(){
		var rec = tSm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择试题.");
			return;
		}
		confirmDelete(remove);
		function remove(){
			ajaxRequest({
				url : "admin/test/delete/" + rec.get("id"),
				callback : function(){
					tStore.reload();
				}
			});
		}
	}
	function update(){
		var rec = tSm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择试题.");
			return;
		}
		var win = genAddorUpdateWin("admin/test/update");
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
	function toggle(){
		var rec =courseSm.getSelected();
		if(rec==null){
			showFailMsg("请选择课程.");
			return;
		}
		ajaxRequest({
			url : "admin/course/toggleStatus/" + rec.get("id"),
			callback : function(){
				courseStore.reload();
			}
		});
	}

	function importIn(){
		var form = new Ext.form.FormPanel({
			fileUpload : true,
			frame : true,
			labelWidth: 30,
			url : "admin/item/importIn",
			defaults : {
				anchor : "100%"
			},
			items : [{
				fieldLabel : "文件",
				emptyText: '请选择文件',
				anchor: '100%',
				allowBlank: false,
				xtype : "fileuploadfield",
				name : 'file',
				buttonText : "浏览"
			}],
			buttons : [{
				text : "确定",
				iconCls : "save",
				handler : function(b){
					submitForm(b.findParentByType("form"), refreshGrid);
					function refreshGrid(){
						itemStore.reload();
					}
				}
			}, {
				text : "重置",
				iconCls : "reset",
				handler : function(b){
					b.findParentByType("form").getForm().reset();
				}
			}]
		});
		var win = new Ext.Window({
			title : "上传文件",
			width : 300,
			height : 100,
			layout : "fit",
			frame : true,
			items : [form]
		});
		win.show();
	}
}

registerModule("TR.admin.trManage.item", TR.admin.trManage.item);